﻿@page "/auditing"
@layout SimpleIdServer.IdServer.Website.Shared.MainLayout
@attribute [Authorize("Authenticated")]
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IState<SearchAuditingRecordsState> searchAuditingRecordsState
@inject IDispatcher dispatcher
@inject DialogService dialogService
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Shared.Dialogs
@using SimpleIdServer.IdServer.Website.Stores.Auditing;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;

<SidAuthorizeView Roles=@($"/auditing/manage,/auditing/view")>
    <Authorized>
        <RadzenText class="mt-3 mb-3 no-margin" Text="@Global.Auditing" TextStyle="TextStyle.DisplayH3" />

        <div class="mb-1">
            <RadzenCheckBox @bind-Value=@displayErrors Change="@(args => DisplayErrors(args))" TValue="bool" />
            <RadzenLabel Text="@Global.DisplayOnlyErrors" Style="margin-left: 8px; vertical-align: middle;" />
        </div>

        <RadzenDataGrid @ref="auditEvtsGrid"
                        AllowFiltering="true"
                        AllowColumnResize="true"
                        AllowAlternatingRows="false"
                        FilterMode="FilterMode.Simple"
                        FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                        AllowSorting="true"
                        PageSize="15"
                        AllowPaging="true"
                        PagerHorizontalAlign="HorizontalAlign.Left"
                        ShowPagingSummary="true"
                        IsLoading="@searchAuditingRecordsState.Value.IsLoading"
                        Count="@searchAuditingRecordsState.Value.Count"
                        Data="@searchAuditingRecordsState.Value.AuditEvents"
                        LoadData="@LoadData"
                        TItem="AuditEvent"
                        ColumnWidth="300px">
            <Columns>
                <RadzenDataGridColumn TItem="AuditEvent" Property="CreateDateTime" Filterable="false" Sortable="true" FormatString="{0:dd/M/yyyy HH:mm:ss}" SortOrder="SortOrder.Descending" Title="@Global.CreateDateTime" Width="80px" />
                <RadzenDataGridColumn TItem="AuditEvent" Property="Description" Filterable="true" Sortable="false" Title="@Global.Description" Width="80px" />
                <RadzenDataGridColumn TItem="AuditEvent" Property="ErrorMessage" Filterable="false" Sortable="false" Title="@Global.ErrorMessage" Width="80px" />
                <RadzenDataGridColumn TItem="AuditEvent" Property="ClientId" Filterable="true" Sortable="false" Title="@Global.ClientIdentifier" Width="80px" />
                <RadzenDataGridColumn TItem="AuditEvent" Property="UserName" Filterable="true" Sortable="false" Title="@Global.UserName" Width="80px" />
                <RadzenDataGridColumn TItem="AuditEvent" Filterable="false" Sortable="false" Title="@Global.Request" Width="80px">
                    <Template Context="data">
                        @if (data.RequestJSON != null)
                        {
                            <RadzenButton Icon="search" Click="@(async (args) => await ShowAudit(data))" />
                        }
                    </Template>
                </RadzenDataGridColumn>
            </Columns>
        </RadzenDataGrid>
    </Authorized>
    <NotAuthorized>
        @Global.NotAuthorized
    </NotAuthorized>
</SidAuthorizeView>

@code {
    RadzenDataGrid<AuditEvent> auditEvtsGrid;
    bool displayErrors;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            if (!searchAuditingRecordsState.Value.IsLoading)
                auditEvtsGrid?.Reload();
        }
    }

    void LoadData(LoadDataArgs args)
    {
        var act = new SearchAuditingRecordsAction { Filter = args.Filter, OrderBy = args.OrderBy, Skip = args.Skip, Take = args.Top, DisplayOnlyErrors = displayErrors };
        dispatcher.Dispatch(act);
    }

    async Task ShowAudit(AuditEvent auditEvt)
    {
        await dialogService.OpenAsync<RequestDialog>(Global.Request, new Dictionary<string, object>
        {
            { "Content", auditEvt.RequestJSON }
        }, new DialogOptions
            {
                Width = "700px",
                Height = "512px",
                Resizable = true,
                Draggable = true
            });        
    }

    void DisplayErrors(bool a)
    {
        displayErrors = a;
        auditEvtsGrid?.Reload();
    }
}